#version 110

uniform	vec3	AmbientLight;
uniform	vec3	Diffuse;
uniform	vec3	Specular;
uniform float	SpecularShininess;

varying float	LightIntensity;
varying vec3	Dir;
varying vec3	Eye;
varying vec3	Normal;
varying vec2	TexCoord;

const float LOG2byDensity2 = 30.0;

void main()
{
	float z = (gl_FragCoord.z / gl_FragCoord.w) / 1200.0;
	float fogFactor = exp2(-LOG2byDensity2 * z * z);
	fogFactor = clamp(fogFactor, 0.0, 1.0);

	vec4 Ia = vec4(AmbientLight, 1.0);
	vec4 Id = vec4(LightIntensity * Diffuse, 1.0);
	vec4 Is = vec4(Specular * pow(max(dot(normalize(reflect(Dir, Normal)), Eye), 0.0), SpecularShininess), 1.0);

	gl_FragColor = mix(vec4(0.1, 0.1, 0.2, 1.0), Ia + Id + Is, fogFactor);
}
